﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;

namespace DAL
{
    public class NhanVienProvider
    {
        public static List<NguoiDung> _LayDuLieuNhanVien()
        {
            List<NguoiDung> lst = new List<NguoiDung>();
            using (QLTTTHEntities context = new QLTTTHEntities())
            {
                var query = (from nd in context.NguoiDungs select nd);

                foreach (var row in query)
                {
                    NguoiDung _nguoidung = new NguoiDung();
                    _nguoidung.MaNV = row.MaNV;
                    _nguoidung.Username = row.Username;
                    _nguoidung.Password = row.Password;
                    _nguoidung.HoTen = row.HoTen;
                    _nguoidung.GioiTinh = row.GioiTinh;
                    _nguoidung.NgaySinh = row.NgaySinh;
                    _nguoidung.Email = row.Email;
                    _nguoidung.DiaChi = row.DiaChi;
                    _nguoidung.SoDT = row.SoDT;
                    _nguoidung.ChucVu = row.ChucVu;
                    _nguoidung.MaNhomND = row.MaNhomND;
                    _nguoidung.TrangThai = row.TrangThai;

                    lst.Add(_nguoidung);
                }
            }

            return lst;
        }

        public static List<NguoiDung> TimDanhSachNhanVien(object a, SoSanh ss)
        {
            var lst = new List<NguoiDung>();
            using (QLTTTHEntities context = new QLTTTHEntities())
            {
                var query = (from nd in context.NguoiDungs select nd);

                foreach (var row in query)
                {
                    var nd = new NguoiDung();
                    nd.MaNV = row.MaNV;
                    nd.Username = row.Username;
                    nd.Password = row.Password;
                    nd.HoTen = row.HoTen;
                    nd.GioiTinh = row.GioiTinh;
                    nd.NgaySinh = row.NgaySinh;
                    nd.Email = row.Email;
                    nd.DiaChi = row.DiaChi;
                    nd.SoDT = row.SoDT;
                    nd.ChucVu = row.ChucVu;
                    nd.MaNhomND = row.MaNhomND;
                    nd.TrangThai = row.TrangThai;

                    if (ss(a, nd) == 0)
                    {
                        lst.Add(nd);
                    }
                }
            }

            return lst;
        }

        public static NguoiDung LayNguoiDungTheoUsername(string username)
        {
            var user = new NguoiDung();
            using (var context = new QLTTTHEntities())
            {
                var query = (from nd in context.NguoiDungs select nd);
                foreach (var row in query)
                {
                    user.MaNV = row.MaNV;
                    user.Username = row.Username;
                    user.Password = row.Password;
                    user.HoTen = row.HoTen;
                    user.GioiTinh = row.GioiTinh;
                    user.NgaySinh = row.NgaySinh;
                    user.Email = row.Email;
                    user.DiaChi = row.DiaChi;
                    user.SoDT = row.SoDT;
                    user.ChucVu = row.ChucVu;
                    user.MaNhomND = row.MaNhomND;
                    user.TrangThai = row.TrangThai;
                    if (user.Username.Equals(username))
                    {
                        return user;
                    }
                }
            }

            return new NguoiDung();
        }


        #region-------------------Xóa nhân viên ------------------------
        public static bool XoaNhanVien(int maNV)
        {

            try
            {
                using (QLTTTHEntities context = new QLTTTHEntities())
                {
                    var nhanvien = context.NguoiDungs.Single(q => q.MaNV.Equals(maNV));
                    context.NguoiDungs.Remove(nhanvien);
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion


        public static bool CapNhatNguoiDung(NguoiDung ngd)
        {
            try
            {
                using (QLTTTHEntities context = new QLTTTHEntities())
                {
                    var query = context.NguoiDungs.Where(u => u.MaNV == ngd.MaNV).First();
                    query.HoTen = ngd.HoTen;
                    query.DiaChi = ngd.DiaChi;
                    query.ChucVu = ngd.ChucVu;
                    query.Email = ngd.Email;
                    query.GioiTinh = ngd.GioiTinh;
                    query.MaNhomND = ngd.MaNhomND;
                    query.NgaySinh = ngd.NgaySinh;
                    query.Password = ngd.Password;
                    query.Username = ngd.Username;
                    query.TrangThai = ngd.TrangThai;
                    query.SoDT = ngd.SoDT;
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static int ThemNguoiDung(NguoiDung nd)
        {
            int maND = -1;
            try
            {
                using (QLTTTHEntities context = new QLTTTHEntities())
                {
                    nd.BienLais = null;
                    context.NguoiDungs.Add(nd);
                    context.SaveChanges();
                    maND = nd.MaNV;
                }
            }
            catch
            {

            }
            return maND;
        }
    }
}
